<template>
  <div
    v-if="!initializing"
    class="vc-app-list-goods-double"
    :style="visual.parseStyle(currNode.currStyle)"
  >
    <div
      v-if="dataList && dataList.length > 0"
      class="vc-app-list-goods-double-list"
    >
      <div
        v-for="(data, index) in dataList"
        :key="index"
        class="vc-app-list-goods-double-item"
        @click="changeItem(data)"
      >
        <div>
          <img v-if='data["图片"]' class="item-image" :src='imageUrl + data["图片"]' alt />
          <cc-icon v-else iconId="018ece4f76dbdc838868" size="120" />
        </div>
        <div class="item-content">
          <div class="item-line">
            <div class="item-name-label" v-if='data["名称标签"]'>{{data["名称标签"]}}</div>
            <div class="item-name ellipsis">{{data["名称"]}}</div>
          </div>
          <div class="item-line">
            <div class="item-label ellipsis">{{(data["标签"] || '').replace(/,/g, ' ')}}</div>
          </div>
          <div class="item-line item-baseline">
            <div class="item-red item-smaller">￥</div>
            <div class="item-red item-bigger">{{tool.getMoneyInt(data["价格"])}}</div>
            <div class="item-red item-smaller item-mr5">.{{tool.getMoneyScale(data["价格"])}}</div>
            <div class="item-red item-smaller item-mr5" v-if='data["价格标签"]'>{{data["价格标签"]}}</div>
            <div class="item-grey item-smaller ellipsis">{{data["价格补充"]}}</div>
          </div>
        </div>
      </div>
    </div>
    <div
      v-else
      class="vc-app-list-goods-double-empty"
      :style="visual.parseStyle(compStyle)"
    >
      <div style="margin: 40rpx;">暂无数据</div>
    </div>
  </div>
</template>

<script setup>
import { ref, inject, getCurrentInstance, nextTick, onMounted } from 'vue';
import visual from "@/visual/js/visual";
import tool from "@/utils/tool";
import EventHandler from "@/visual/js/event";

const props = defineProps({
  node: Object
});

const initializing = ref(true);
const currNode = ref(props.node);
const compStyle = ref({});
const page = ref({});
const dataList = ref([]);
const currValue = ref('');
const loading = ref('');
const eventHandler = new EventHandler(
  inject('eventHandler', Function, true),
  getCurrentInstance(),
  currNode.value
);
const dataPage = ref(false);
const hasPage = ref(true);
const queryInfo = ref({
  pageNumber: 1,
  pageSize: 10
});
const imageUrl = ref(import.meta.env.VITE_IMAGE_PATH);

const init = async(styleId) => {
  initializing.value = true;
  page.value = {};
  dataList.value = [];
  queryInfo.value.pageNumber = 1;
  hasPage.value = true;
  
  visual.initStyle(currNode.value, styleId);
  compStyle.value = currNode.value.currCompStyle || {};
  const compData = currNode.value.compData || {};
  dataPage.value = compData.dataPage || false;
  if (dataPage.value) {
    queryInfo.value.pageSize = compData.dataPageSize || 20;
  } else {
    queryInfo.value.pageSize = compData.dataQueryTotal || 20;
  }
  await findPage();
  await nextTick(() => initializing.value = false);
};

const changeItem = (row) => {
  currValue.value = row['主键id'] || '';
  eventHandler.trigger('点击事件', {'row': row});
};

const findPage = async() => {
  loading.value = true;
  const compData = currNode.value.compData || {};
  page.value = await visual.loadData(compData, eventHandler, getData(), currNode.value);
  if (page.value.data && page.value.data.length > 0) {
    dataList.value.push(...page.value.data);
  }
  if ((page.value.totalPage && page.value.totalPage <= queryInfo.value.pageNumber) 
      || !page.value.data || page.value.data.length === 0) {
    hasPage.value = false;
  }
  await nextTick(() => loading.value = false);
}

const getData = () => {
  return { 
    ...queryInfo.value,
    value: currValue.value || '',
    inputParam: currNode.value.inputParam || {} 
  };
};

const changeStyle = (styleId) => {
  init(styleId);
};

const refresh = () => {
  init();
};

onMounted(() => {
  refresh();
});

defineExpose({ getData, changeStyle, refresh });
</script>

<style scoped lang="scss">
.vc-app-list-goods-double {
  width: 100%;
  display: flex;
  flex-direction: row;
  background: #F5F6FA;
  .vc-app-list-goods-double-list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    overflow: auto;
    padding: 20rpx;
    gap: 20rpx;
    .vc-app-list-goods-double-item {
      white-space: nowrap;
      width: calc(50vw - 30rpx);
      display: flex;
      flex-direction: column;
      border-radius: 10rpx;
      background: white;
      padding-bottom: 6rpx;
      .item-image, image {
        max-width: calc(50vw - 30rpx);
        max-height: calc(50vw - 30rpx);
        border-radius: 10rpx 10rpx 0 0;
      }
      .item-content {
        padding: 10rpx;
        .ellipsis {
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
        }
        .item-red {
          color: #FF3333;
        }
        .item-grey {
          color: #8c939d;
        }
        .item-smaller {
          font-size: 24rpx;
        }
        .item-bigger {
          font-size: 40rpx;
          font-weight: bold;
        }
        .item-baseline {
          align-items: baseline!important;
        }
        .item-line {
          display: flex;
          flex-direction: row;
          align-items: center;
        }
        .item-name-label {
          background: #FF3333;
          color: white;
          font-size: 20rpx;
          border-radius: 10rpx;
          padding: 4rpx 10rpx;
          margin-right: 10rpx;
        }
        .item-name {
          flex-grow: 1;
          width: 1rpx;
        }
        .item-label {
          color: #D97E23;
          font-size: 24rpx;
          height: 50rpx;
          line-height: 50rpx;
        }
        .item-mr5 {
          margin-right: 10rpx;
        }
      }
    }
  }
  .vc-app-list-goods-double-list::-webkit-scrollbar {
    display: none;
  }
  .vc-app-list-goods-double-icon {
    width: 60rpx;
    margin-left: 20rpx;
    border-left: 1rpx solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .vc-app-list-goods-double-empty {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.vc-loading {
  background: #F5F6FA;
}
</style>